package com.wormhole.wormholemanagement.utils;

import com.drew.imaging.ImageMetadataReader;
import com.drew.imaging.ImageProcessingException;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

public class PicExifInfoUtil {
    public static void main(String[] args) throws ImageProcessingException, IOException {
        String imgDir = "D:\\相片档案\\2025\\9月\\0901\\阅兵\\";
        File[] files = new File(imgDir).listFiles();
        for (File file : files) {
            if (!file.getName().toLowerCase().endsWith(".jpg")) {
                continue;
            }
            System.out.println("----------------------------------------\n" + file.getName());
            // 获取照片信息
            Map exifMap = readPicExifInfo(file);
            System.out.println(exifMap);

        }
    }

    /**
     * 获取图片文件的Exif信息
     *
     * @param file
     * @return
     * @throws ImageProcessingException
     * @throws IOException
     */
    public static Map<String, String> readPicExifInfo(File file) throws ImageProcessingException, IOException {
        Map<String, String> map = new HashMap<>();
        Metadata metadata = ImageMetadataReader.readMetadata(file);
        for (Directory directory : metadata.getDirectories()) {
            for (Tag tag : directory.getTags()) {
                // 输出所有属性
                System.out.format(
                        "[%s] - %s = %s\n", directory.getName(), tag.getTagName(), tag.getDescription());
                map.put(tag.getTagName(), tag.getDescription());
            }
            if (directory.hasErrors()) {
                for (String error : directory.getErrors()) {
                    System.err.format("ERROR: %s", error);
                }
            }
        }
        return map;
    }
}
